package chattingRoomUpdate;

import chattingRoom.ReleaseUtils;

import java.io.*;
import java.net.Socket;
import java.util.Random;
import java.util.Scanner;

/**
 * 客户端的发送方法
 */
public class ClientSend  implements Runnable{

    private DataOutputStream dataOutputStream;
    private Socket client;
    private boolean isRunning;
    private Scanner scanner;
    private String username;
    private boolean isFile;
    private Thread thread;

    public ClientSend(Socket client, String username){
        this.client=client;
        try {
            dataOutputStream=new DataOutputStream(client.getOutputStream());
            this.username=username;
            //当管道连接成功之后，就将用户名发送过去
            sendMessage(username);
            /**
             * 注意 就因为这个地方没写true底下的send方法根本就没跑的起来，搞了我半天
             */
            isRunning=true;
            scanner=new Scanner(System.in);
        } catch (IOException e) {
            release();
            System.out.println("客户端发送模块出现了问题");
        }
    }

    private void sendMessage(String message){
        try {
            dataOutputStream.writeUTF(message);
            dataOutputStream.flush();
        } catch (IOException e) {
            release();
            System.out.println("发送信息的过程出现了错误");
        }
    }

    private void release(){
        this.isRunning=false;
        this.isFile=false;
        ReleaseUtils.close(dataOutputStream,client);
    }

    @Override
    public void run() {
        for (;isRunning!=false;) {
            try {
                thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Scanner input = new Scanner(System.in);
            System.out.println("是否上传文件？是：否?");
            System.out.println("请输入是或者否");
            String c = input.nextLine();

                switch (c) {
                    case ("否"): {
                        //读取控制台的输入信息
                        //这里使用scanner可能你在输入台的信息之间不能有空格  你可以使用buffer Reader进行使用
                        System.out.println("请输入聊天内容:");
                        String message = scanner.next();
                        if (!message.equals("")) {
                            sendMessage(message);
                        }
                        break;
                    }
                    case ("是"): {
                        this.isFile=true;
                        if (isFile) {
                            //子类重写run方法的时候不能直接抛出异常那么就trycatch出来
                            try {
                                System.out.println("图片上传成功");
                                sendMessage(username+"上传了文件，请查看!.png");
                                InputStream inputStream = client.getInputStream();
                                File file = new File("C:\\Users\\鱼尾\\Desktop\\socketChattingRoomSimple\\files");
                               //表示该file对象不为空的话去创建相关的目录
                                if (!file.exists()) {
                                    file.mkdir();
                                }
                                byte[] bytes = new byte[1024];
                                int len = 0;
                                String filename = System.currentTimeMillis() + new Random().nextInt(99999) + ".png";
                                // FileOutputStream fileOutputStream = new FileOutputStream(file + "\\1.txt");
                                //名称不用写死
                                FileOutputStream fileOutputStream = new FileOutputStream(file + "\\" + filename);
                                //将得到的信息传入文件夹的指定位置
                                while ((len = inputStream.read(bytes)) != -1) {
                                    fileOutputStream.write(bytes, 0, len);
                                }
                                //返回给客户端信息
                                client.getOutputStream().write("已经成功收到".getBytes());

                                inputStream.close();
                                fileOutputStream.close();
                            } catch (IOException e) {
                                System.out.println(e);
                            }
                            break;
                        }
                    }
                }
            }
        }
    }

